﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace WindControl
{
    public partial class MyWindControl : UserControl
    {
        private float rotationAngle = 0f;
        private Timer rotationTimer;
        private Color outlineColor = Color.LimeGreen;
        private float lineThickness = 2.5f;
        private bool isRotating = true; // 默认旋转状态

        public MyWindControl()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint |
                     ControlStyles.UserPaint |
                     ControlStyles.OptimizedDoubleBuffer, true);

            Size = new Size(150, 150);
            BackColor = Color.Transparent;
            DoubleBuffered = true;

            // 初始化旋转计时器
            rotationTimer = new Timer { Interval = 25 };
            rotationTimer.Tick += RotationTimer_Tick;
            rotationTimer.Start();
        }

        private void RotationTimer_Tick(object sender, EventArgs e)
        {
            if (isRotating)
            {
                rotationAngle = (rotationAngle + 8f) % 360;
                Invalidate();
            }
        }

        // 轮廓颜色属性
        public Color OutlineColor
        {
            get => outlineColor;
            set { outlineColor = value; Invalidate(); }
        }

        // 线宽属性
        public float LineThickness
        {
            get => lineThickness;
            set { lineThickness = value; Invalidate(); }
        }

        // 旋转状态属性
        public bool IsRotating
        {
            get => isRotating;
            set
            {
                isRotating = value;
                Invalidate(); // 重绘以反映状态变化
            }
        }

        // 公共方法：启动旋转
        public void StartRotation()
        {
            IsRotating = true;
        }

        // 公共方法：停止旋转
        public void StopRotation()
        {
            IsRotating = false;
        }

        // 公共方法：切换旋转状态
        public void ToggleRotation()
        {
            IsRotating = !IsRotating;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // 计算中心点和尺寸
            int centerX = Width / 2;
            int centerY = Height / 2;
            int size = Math.Min(Width, Height) - 10;
            int radius = size / 2;

            // 使用高质量笔刷
            using (var outlinePen = new Pen(outlineColor, lineThickness))
            {
                outlinePen.StartCap = LineCap.Round;
                outlinePen.EndCap = LineCap.Round;
                outlinePen.LineJoin = LineJoin.Round;

                // 绘制外壳
                DrawHousing(g, outlinePen, centerX, centerY, radius);

                // 绘制旋转风叶
                DrawBlades(g, outlinePen, centerX, centerY, radius - 20);

                // 绘制中心点
                g.DrawEllipse(outlinePen, centerX - 8, centerY - 8, 16, 16);

                // 绘制旋转状态指示器
                //DrawRotationIndicator(g, centerX, centerY, radius);
            }
        }

        private void DrawHousing(Graphics g, Pen pen, int centerX, int centerY, int radius)
        {
            // 外壳轮廓
            g.DrawEllipse(pen, centerX - radius, centerY - radius, radius * 2, radius * 2);

            // 内部装饰环
            /*using (var innerPen = new Pen(outlineColor, lineThickness * 0.7f))
            {
                innerPen.DashStyle = DashStyle.Dash;
                g.DrawEllipse(innerPen, centerX - radius + 25, centerY - radius + 25,
                             radius * 2 - 50, radius * 2 - 50);
            }

            // 螺丝纹路（轮廓线）
            for (int i = 0; i < 8; i++)
            {
                float angle = i * 45;
                float rad = angle * (float)Math.PI / 180;

                Point start = new Point(
                    centerX + (int)((radius - 15) * Math.Cos(rad)),
                    centerY + (int)((radius - 15) * Math.Sin(rad)));

                Point end = new Point(
                    centerX + (int)((radius - 35) * Math.Cos(rad)),
                    centerY + (int)((radius - 35) * Math.Sin(rad)));

                g.DrawLine(pen, start, end);
            }*/
        }

        private void DrawBlades(Graphics g, Pen pen, int centerX, int centerY, int bladeLength)
        {
            // 保存图形状态
            GraphicsState state = g.Save();
            g.TranslateTransform(centerX, centerY);
            g.RotateTransform(rotationAngle);

            // 绘制三片带弧度的风叶轮廓
            for (int i = 0; i < 3; i++)
            {
                DrawBlade(g, pen, bladeLength);
                g.RotateTransform(120);
            }

            // 恢复图形状态
            g.Restore(state);
        }

        private void DrawBlade(Graphics g, Pen pen, int length)
        {
            // 创建带弧度的风叶轮廓路径
            using (GraphicsPath path = new GraphicsPath())
            {
                // 优雅的曲线点 - 形成空气动力学弧度
                PointF[] points = {
                    new PointF(0, 0),
                    new PointF(-length * 0.2f, -length * 0.1f),
                    new PointF(-length * 0.4f, -length * 0.2f),
                    new PointF(-length * 0.6f, -length * 0.25f),
                    new PointF(-length * 0.8f, -length * 0.2f),
                    new PointF(-length, 0),
                    new PointF(-length * 0.8f, length * 0.2f),
                    new PointF(-length * 0.6f, length * 0.25f),
                    new PointF(-length * 0.4f, length * 0.2f),
                    new PointF(-length * 0.2f, length * 0.1f),
                    new PointF(0, 0)
                };

                path.AddCurve(points, 0.5f);

                // 只绘制轮廓，不填充
                //g.DrawPath(pen, path);
                // 填充风叶
                using (var bladeBrush = new SolidBrush(outlineColor))
                {
                    g.FillPath(bladeBrush, path);
                }

                // 风叶结构线 - 加强轮廓表现
                using (var detailPen = new Pen(outlineColor, lineThickness * 0.5f))
                {
                    // 主弧线
                    PointF[] curve = {
                        new PointF(-length * 0.15f, 0),
                        new PointF(-length * 0.4f, -length * 0.1f),
                        new PointF(-length * 0.7f, -length * 0.12f),
                        new PointF(-length * 0.9f, 0)
                    };
                    g.DrawCurve(detailPen, curve, 0.5f);

                    // 连接线
                    g.DrawLine(detailPen, -length * 0.6f, -length * 0.25f, -length * 0.6f, length * 0.25f);
                }
            }
        }

        private void DrawRotationIndicator(Graphics g, int centerX, int centerY, int radius)
        {
            // 在右下角绘制旋转状态指示器
            int indicatorSize = 20;
            int indicatorX = centerX + radius - indicatorSize - 10;
            int indicatorY = centerY + radius - indicatorSize - 10;

            using (var indicatorPen = new Pen(outlineColor, 2f))
            {
                // 绘制外圈
                g.DrawEllipse(indicatorPen, indicatorX, indicatorY, indicatorSize, indicatorSize);

                if (isRotating)
                {
                    // 旋转状态 - 绘制三个旋转点
                    for (int i = 0; i < 3; i++)
                    {
                        float angle = i * 120 + rotationAngle;
                        float rad = angle * (float)Math.PI / 180;

                        int pointSize = 4;
                        int pointX = indicatorX + indicatorSize / 2 +
                                    (int)((indicatorSize / 2 - 3) * Math.Cos(rad));
                        int pointY = indicatorY + indicatorSize / 2 +
                                    (int)((indicatorSize / 2 - 3) * Math.Sin(rad));

                        g.FillEllipse(Brushes.White, pointX - pointSize / 2, pointY - pointSize / 2,
                                     pointSize, pointSize);
                        g.DrawEllipse(indicatorPen, pointX - pointSize / 2, pointY - pointSize / 2,
                                     pointSize, pointSize);
                    }
                }
                else
                {
                    // 停止状态 - 绘制停止符号
                    int barWidth = 4;
                    int barHeight = indicatorSize - 8;
                    g.FillRectangle(Brushes.White,
                                   indicatorX + (indicatorSize - barWidth) / 2,
                                   indicatorY + (indicatorSize - barHeight) / 2,
                                   barWidth, barHeight);
                }
            }
        }

        public void SetRotationSpeed(float speed)
        {
            rotationTimer.Interval = (int)(25 / (speed / 3f));
        }

        /*protected override void Dispose(bool disposing)
        {
            if (disposing && rotationTimer != null)
            {
                rotationTimer.Stop();
                rotationTimer.Dispose();
            }
            base.Dispose(disposing);
        }*/

    }
}
